﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BuildPlannerLib.PlayerActions;
using BuildPlannerLib.SimEvents;
using BuildPlannerLib.Enums;
using BuildPlannerLib.Buildings;

namespace BuildPlannerLib.Terran.PlayerActions
{
    // Derived class from Action for producing a Refinery
    public class ProduceRefinery : PlayerAction
    {
        // Field for the base from which the SCV will be taken
        public Base Base;

        // Field for the per-trip yield of the geyser that will be used for the constructing SCV when it starts mining
        public int Yield;

        // Constructor with parameters for base and build
        public ProduceRefinery(Build build, Base b)
        {
            Build = build;
            Base = b;
            MineralCost = 75;
            TimeDuration = 3000;
            Description = "Produce Refinery (Base " + Build.Map.Bases.IndexOf(Base) + ")";
            LongDescription = "Producing a Refinery at base number " + Build.Map.Bases.IndexOf(Base) + ".";
        }

        // Evaluates whether the player is Terran, whether there is mineral income, and whether there is a geyser on which to place a Refinery
        public override bool RequirementsFilled()
        {
            Error = null;

            if (Build.Race != Race.Terran)
            {
                Error = "This building can only be built by Terran players.";
                return false;
            }

            if (Base.MineralWorkers == 0)
            {
                Error = "No workers assigned to minerals at the specified base.";
                return false;
            }

            if (Base.GasGeysers.Sum() == Base.StartedGasGeysers.Sum())
            {
                Error = "No un-converted gas geysers available.";
                return false;
            }

            return base.RequirementsFilled();
        }

        // Evaluates if enough minerals are available and an SCV is available build the structure
        public override bool StartCheck()
        {
            if (Base.MineralWorkerStatus.Sum() == 0)
                return false;

            return base.StartCheck();
        }

        // Spends minerals, removes a worker to build the building, starts building the Refinery, and adds the event to finish the action
        public override void StartAction()
        {
            base.StartAction();

            Base.StartGasConversion();
            Base.RemoveMineralWorker(ref Build);
            Build.Player.IdleWorkers++;
        }

        // Adds a Refinery, makes the geyser available for gas mining, and adds the event to start the worker mining gas
        public override void FinishAction()
        {
            NonProduction refinery = Build.Player.GetBuilding("Refinery") as NonProduction;

            base.FinishAction();

            if (Base.StartedGasGeysers[2] > Base.ConvertedGasGeysers[2])
                Yield = 8;
            else if (Base.StartedGasGeysers[1] > Base.ConvertedGasGeysers[1])
                Yield = 6;
            else
                Yield = 4;

            refinery.AddBuilding();
            Base.FinishGasConversion();
            Build.Player.IdleWorkers--;
            Base.GasWorkers++;
            int eventTime = TimeConvert.ToInt(Build.CurrentTime) + 211;
            Build.EventList.Add(new FinishGasMining(Build, Base, Yield, eventTime));
        }
    }
}
